/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package rie07;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author JBP
 */
public class User {
    private Connection connect = null;
    private PreparedStatement preparedStatement=null;
    private Statement statement;
    private ResultSet resultSet;
    private String nom, mdp, email;
        //ecriture BDD
    private String equip, adresse;

    public String getEquip() {
        return equip;
    }

    public void setEquip(String equip) {
        this.equip = equip;
    }

    public String getAdresse() {
        return adresse;
    }

    public void setAdresse(String adresse) {
        this.adresse = adresse;
    }
    
    
    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getMdp() {
        return mdp;
    }

    public void setMdp(String mdp) {
        this.mdp = mdp;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
    
    public User(){
        
    }
        public void connect(){
        try {
            //Class.forName("com.mysql.jdbc.Driver");
            Class.forName("org.apache.derby.jdbc.ClientDriver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Dispatcher.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {        
            
            connect = DriverManager.getConnection("jdbc:derby://localhost:1527/madb;user=root;password=r00t");
            System.out.println("Connexion OK");
        } catch (SQLException ex) {
            Logger.getLogger(Dispatcher.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
        
        public void addUser(String name, String mdp){
            connect();
            try {
                preparedStatement = connect.prepareStatement(
                        "insert into ROOT.users (nom, mdp) values ('"+name+"','"+mdp+"')");
            } catch (SQLException ex) {
                Logger.getLogger(Dispatcher.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                preparedStatement.executeUpdate();
            } catch (SQLException ex) {
                Logger.getLogger(Dispatcher.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        public void addIP(String nom, String ip){
            connect();
            try {
                preparedStatement = connect.prepareStatement(
                        "insert into ROOT.adresseip (name, adresse) values ('"+nom+"','"+ip+"')");
            } catch (SQLException ex) {
                Logger.getLogger(Dispatcher.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                preparedStatement.executeUpdate();
            } catch (SQLException ex) {
                Logger.getLogger(Dispatcher.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        public ArrayList getIP(){
            ArrayList<String> temp = new ArrayList<String>();
            connect();
            try {
                statement = connect.createStatement();
                resultSet = statement.executeQuery(
                            "select adresse from ROOT.adresseip");

                while(resultSet.next()){
                    temp.add(resultSet.getString("adresse"));
                }
                } catch (SQLException ex) {
                Logger.getLogger(Dispatcher.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            return temp;
        }
        
}
